package cn.com.zhbs.account.controller;

import cn.com.zhbs.account.entitys.Account;
import cn.com.zhbs.account.service.DepartmentService;
import cn.com.zhbs.common.Result.SimpleFactory.ResultFactory;
import com.alibaba.fastjson.JSONObject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Map;

/**
 * @program: vr_manage
 * @description: 机构管理
 * @author: 陈治杰
 * @create: 2021-08-07 14:06
 **/
@Controller
@RequestMapping("department")
public class DepartmentController {
    @Resource
    DepartmentService departmentService;

    //新增机构
    @RequestMapping(value = "addDepartment", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    @ResponseBody
    public Map<String,Object> addDepartment(@RequestBody JSONObject jsonObject, HttpServletRequest request){
        HttpSession session=request.getSession();
        try{
            jsonObject.put(Account.aid.toString(),session.getAttribute(Account.aid.toString()));
            return departmentService.addDepartment(jsonObject);
        }catch (Exception e){
            e.printStackTrace();
        }
        return ResultFactory.failure().display();
    }

    //机构列表
    @RequestMapping(value = "departmentList", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    @ResponseBody
    public Map<String,Object> departmentList(@RequestBody JSONObject jsonObject,HttpServletRequest request){
        HttpSession session=request.getSession();
        try{
            jsonObject.put(Account.aid.toString(),session.getAttribute(Account.aid.toString()));
            jsonObject.put(Account.superRole.toString(),session.getAttribute(Account.superRole.toString()));
            return ResultFactory.success(departmentService.departmentList(jsonObject)).display();
        }catch (Exception e){
            e.printStackTrace();
        }
        return ResultFactory.failure().display();
    }

    //编辑机构
    @RequestMapping(value = "editDepartment" , method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    @ResponseBody
    public Map<String,Object> editDepartment(@RequestBody JSONObject jsonObject,HttpServletRequest request){
        try{
            departmentService.editDepartment(jsonObject);
            return ResultFactory.success().display();
        }catch (Exception e){
            e.printStackTrace();
        }
        return ResultFactory.failure().display();
    }

    //机构详情
    @RequestMapping(value = "departmentDetail", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    @ResponseBody
    public Map<String,Object> departmentDetail(@RequestBody JSONObject jsonObject,HttpServletRequest request){
        try{
            return ResultFactory.success(departmentService.departmentDetail(jsonObject)).display();
        }catch (Exception e){
            e.printStackTrace();
        }
        return ResultFactory.failure().display();
    }

    //删除机构
    @RequestMapping(value = "deleteDepartment" , method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    @ResponseBody
    public Map<String,Object> deleteDepartment(@RequestBody JSONObject jsonObject,HttpServletRequest request){
        try{
            departmentService.deleteDepartment(jsonObject);
            return ResultFactory.success().display();
        }catch (Exception e){
            e.printStackTrace();
        }
        return ResultFactory.failure().display();
    }
}
